package com.example.day6animation;

import android.animation.AnimatorSet;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TweenActivity extends AppCompatActivity {

    @BindView(R.id.bt1)
    Button bt1;
    @BindView(R.id.bt2)
    Button bt2;
    @BindView(R.id.bt3)
    Button bt3;
    @BindView(R.id.bt4)
    Button bt4;
    @BindView(R.id.iv)
    ImageView iv;
    @BindView(R.id.bt5)
    Button bt5;
    @BindView(R.id.bt6)
    Button bt6;
    @BindView(R.id.bt7)
    Button bt7;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tween);
        ButterKnife.bind(this);

    }

    @OnClick({R.id.bt1, R.id.bt2, R.id.bt3, R.id.bt4, R.id.iv, R.id.bt5, R.id.bt6,R.id.bt7})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt1:  //平移
                TranslateAnimation animation = (TranslateAnimation) AnimationUtils.loadAnimation(this, R.anim.t_animation);
                iv.startAnimation(animation);
                break;
            case R.id.bt2:  //旋转
                RotateAnimation animation1 = (RotateAnimation) AnimationUtils.loadAnimation(this, R.anim.r_animition);
                animation1.setFillAfter(true); //停留在动画之后
                animation1.setRepeatMode(Animation.REVERSE);  //设置重复模式
                animation1.setRepeatCount(2);
                iv.startAnimation(animation1);
                break;
            case R.id.bt3:  //缩放
                ScaleAnimation animation2 = (ScaleAnimation) AnimationUtils.loadAnimation(this, R.anim.s_animation);
                iv.startAnimation(animation2);
                break;
            case R.id.bt4: //透明度
                AlphaAnimation animation3 = (AlphaAnimation) AnimationUtils.loadAnimation(this, R.anim.a_animation);
                iv.startAnimation(animation3);
                break;
            case R.id.bt5: //组合动画
                AnimationSet animationSet = new AnimationSet(true);
                animationSet.addAnimation(AnimationUtils.loadAnimation(this,R.anim.r_animition));
                animationSet.addAnimation(AnimationUtils.loadAnimation(this,R.anim.a_animation));
                animationSet.addAnimation(AnimationUtils.loadAnimation(this,R.anim.s_animation));
                animationSet.setDuration(700);
                animationSet.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        Toast.makeText(TweenActivity.this, "onAnimationStart", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        Toast.makeText(TweenActivity.this, "onAnimationEnd", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                        Toast.makeText(TweenActivity.this, "onAnimationRepeat", Toast.LENGTH_SHORT).show();
                    }
                });
                iv.startAnimation(animationSet);
                break;
            case R.id.bt6:   //在java中编写
                TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 0, iv.getHeight() * 2);
                translateAnimation.setDuration(500);
                iv.startAnimation(translateAnimation);
                break;
            case R.id.bt7:  //过场动画
                finish();
                break;
            case R.id.iv:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.back_in,R.anim.back_out);
    }
}
